﻿//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
//#include <string.h>
//#include <math.h>

#include "common.h"

void triangle_judge(char* result)
{ // 输入三个整数，判断三角形类型
    int a = 0, b = 0, c = 0;
    printf("Enter three sides of a triangle:\n ");
    int num = scanf("%d %d %d", &a, &b, &c);
    while ('\n' != getchar())
        continue; // 清理输入缓冲区
    if (0 >= a || 0 >= b || 0 >= c)
    {
        printf("The triangle is not valid.\n");
        strcpy(result, "输入错误");
    }
    else if (a + b <= c || a + c <= b || b + c <= a)
    {
        printf("cannot form a triangle.\n");
        strcpy(result, "不能构成三角形");
    }
    else if (a == b && b == c)
    {
        printf("The triangle is equilateral.\n");
        strcpy(result, "等边三角形");
    }
    else if (a == b || b == c || a == c)
    {
        if (a * a + b * b == c * c ||
            a * a + c * c == b * b || b * b + c * c == a * a)
        {
            printf("The triangle is isosceles and right-angled.\n");
            strcpy(result, "等腰直角三角形");
        }
        else
        {
            printf("The triangle is isosceles.\n");
            strcpy(result, "等腰三角形");
        }
    }
    else if (a * a + b * b == c * c ||
        a * a + c * c == b * b || b * b + c * c == a * a)
    {
        printf("The triangle is right-angled.\n");
        strcpy(result, "直角三角形");
    }
    else
    {
        printf("The triangle is ordinary.\n");
        strcpy(result, "普通三角形");
    }
}

void reverse_string(char* str)
{ // 输入字符串，输出反转后的字符串，不使用库函数
    char* start = str;
    char* end = str;
    while (*end++ != '\0'); // 到字符串的结尾 '\0' + 1 位置
    end -= 2;
    char temp;
    while (start < end)
    {
        temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

int sum_like_num(int value, int num)
{ //  计算一个同值数列前 n 项之和，
    // 如 value = 2，num = 5 则输出 2 + 22 + 222 + 2222 + 22222
    if (num == 0)
        return 0;
    int sum = value;
    int i;
    int temp = value;
    for (i = 1; i < num; ++i)
    {
        value *= 10;
        value += temp; // 写为 value = value * 10 + temp; 也行
        sum += value;
    }
    return sum;
}

int narcissistic(int start, int end)
{// 判断从 start 到 end 之间（正整数范围）的所有的水仙花数
    // 水仙花数是指比如一个 3 位数，其各位数字的 3 次方之和等于该数本身
    if (start < 0 || end < 0 || start > end)
    {
        printf("Invalid input.\n");
        return -1; // 输入错误
    }

    int i, count = 0;
    for (i = start; i <= end; ++i)
    {
        int j = i, post = 1;
        while (j /= 10)
            ++post; // 计算 i 的位数

        int sum = 0;
        j = i; // 重置 j
        while (j)
        {
            sum += (int)pow(j % 10, post);// 计算各位数的 post 次方之和
            j /= 10;
        }
        if (sum == i)
        {
            ++count;
            printf("%d ", i);
        }
    }
    printf("\n");
    return count;
}

void print_diamond(int hight)
{ // 打印菱形，n 为菱形的行数
    int i, j;
    hight = 1 == (hight % 2) ? hight : hight - 1; // 保证 hight 为奇数
    for (i = 0; i <= hight / 2; ++i)
    {
        for (j = 0; j < hight / 2 - i; ++j)
            printf(" ");
        for (j = 0; j < 2 * i + 1; ++j)
            printf("*");
        printf("\n");
    }

    for (i = 0; i < hight - hight / 2 - 1; ++i)
    {
        for (j = 0; j < i + 1; ++j)
            printf(" ");
        for (j = 0; j < hight - 2 * i - 2; ++j)
            printf("*");
        printf("\n");
    }
}

int empty_bottle(int price, int num, int money)
{ // 喝汽水问题：例如一瓶汽水一元，两个空瓶换一瓶汽水，问 20 块钱能喝多少瓶汽水？
    int count_buy = money / price; // 先计算能购买多少瓶
    if (count_buy < num) // 兑换量不足
        return count_buy;
    int count_change = count_buy / num; // 换汽水数
    int remain = count_change + count_buy % num; // 计算剩余空瓶数
    int change_sum = count_change;
    while (remain >= num)
    {
        count_change = remain / num; // 计算一次换汽水可以换多少瓶
        remain = count_change + remain % num; // 计算剩余空瓶数
        change_sum += count_change; // 累计换汽水总数
    }
    return change_sum + count_buy; // 最后的换汽水总数
}
